// 输入：nums = [-4, -1, 0, 3, 10]
// 输出：[0, 1, 9, 16, 100]
// 解释：平方后，数组变为[16, 1, 0, 9, 100]
// 排序后，数组变为[0, 1, 9, 16, 100]

let nums = [-4, -1, 0, 3, 10]

var sortedSquares = function (nums) {
    let result = []
    let k = nums.length - 1
    for (let i = 0, j = k; i <= j;) {
        if (nums[i] * nums[i] > nums[j] * nums[j]) {
            result[k--] = nums[i] * nums[i]
            i++
        } else {
            result[k--] = nums[j] * nums[j]
            j--
        }
    }
    return result
};

console.log(sortedSquares(nums));
